{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "1、python线程\n",
    "1.1 通过函数调用实现\n",
    "1.2 通过继承实现\n",
    "2 threading、_thraed"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import threading\n",
    "def add(x,y):\n",
    "    print(f\"{x}*{y} = {x*y}\")\n",
    "\n",
    "def fun(n):\n",
    "    for i in range(n):\n",
    "        print(threading.current_thread().name, i)\n",
    "# 线程\n",
    "t1 = threading.Thread(target=fun, args=(10, ))\n",
    "# 启动线程1\n",
    "t1.start()\n",
    "\n",
    "t2 = threading.Thread(target=fun, args=(5, ))\n",
    "t2.start()\n",
    "\n",
    "                                                                                        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "12 中华田园犬 旺财\n"
     ]
    }
   ],
   "source": [
    "# 通过继承实现线程\n",
    "class Animal:\n",
    "    \n",
    "    # def __init__(self) -> None:\n",
    "    #     self.id = 0\n",
    "    #     self.type = \"动物\"\n",
    "    \n",
    "    def __init__(self, id, type) -> None:  # 方法的重载\n",
    "        self.id = id\n",
    "        self.type = type\n",
    "        \n",
    "    def show(self):\n",
    "        print(f\"{self.id} {self.type}\")\n",
    "\n",
    "# an = Animal()\n",
    "# an.show()\n",
    "\n",
    "class Dog(Animal):\n",
    "    def __init__(self, id, type, name) -> None:\n",
    "        super().__init__(id, type)\n",
    "        self.name = name\n",
    "    \n",
    "    def show(self):  # 方法的重写\n",
    "        print(f\"{self.id} {self.type} {self.name}\")\n",
    "\n",
    "dog = Dog(12, \"中华田园犬\", \"旺财\")\n",
    "dog.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Thread-45 0\n",
      "Thread-46 0\n",
      "Thread-47 0\n",
      "Thread-48 Thread-49 0\n",
      "0\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Thread-47Thread-46 1\n",
      "Thread-45  1\n",
      "1\n",
      "Thread-48Thread-49 1\n",
      " 1\n",
      "Thread-45Thread-47Thread-46  2\n",
      " 2\n",
      "2\n",
      "Thread-48Thread-49  2\n",
      "2\n",
      "Thread-45 3Thread-47 3\n",
      "Thread-46 3\n",
      "\n",
      "Thread-49Thread-48 3\n",
      " 3\n",
      "Thread-47Thread-45 4 4\n",
      "\n",
      "Thread-46 4\n",
      "Thread-49Thread-48 4\n",
      " 4\n",
      "Thread-46Thread-47Thread-45 5\n",
      " 5\n",
      " 5\n",
      "Thread-48Thread-49 5\n",
      " 5\n",
      "Thread-45Thread-47Thread-46 6\n",
      " 6\n",
      " 6\n",
      "Thread-48Thread-49  6\n",
      "6\n",
      "Thread-47Thread-45Thread-46 7 7\n",
      "\n",
      " 7\n",
      "Thread-49Thread-48 7\n",
      " 7\n",
      "Thread-47 8\n",
      "Thread-45Thread-46 8\n",
      "Thread-48 8\n",
      "Thread-49  88\n",
      "\n",
      "Thread-47 9\n",
      "Thread-46Thread-48Thread-49Thread-45 9\n",
      " 9\n",
      " 9\n",
      " 9\n"
     ]
    }
   ],
   "source": [
    "from threading import Thread\n",
    "import threading\n",
    "import time\n",
    "\n",
    "# 继承\n",
    "class MyThread(Thread):\n",
    "   def run(self) -> None: # 重写\n",
    "     super().run()\n",
    "     # 自定义任务\n",
    "     for i in range(10):\n",
    "        print(threading.current_thread().name, i)\n",
    "        time.sleep(1)\n",
    "\n",
    "for i in range(5):\n",
    "   thread = MyThread()\n",
    "   thread.start() # 线程启动\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "pytorch",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
